﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using iMapa.UI.ModelView.Common;
using iMapa.Helper;

namespace iMapa.UI.ModelView.Main.Wizard
{
    public class SelectionAreaViewModel : ViewModelBase
    {
        public enum FunctionMode { opt0, crop, transform }

        private FunctionMode mode;
        private double x1off;
        private double x2off;
        private double x3off;
        private double x4off;
        private double y1off;
        private double y2off;
        private double y3off;
        private double y4off;

        private double offset;
        private PointCollection points;

        private double initpointw, initpointh;

        public SelectionAreaViewModel(double parentwidth, double parentheight, double offset)
        {
            initpointw = parentwidth;
            initpointh = parentheight;
            this.offset = offset;
            initArea(parentwidth, parentheight);
            Point Point1 = new Point(X1off, Y1off);
            Point Point2 = new Point(X2off, Y2off);
            Point Point3 = new Point(X3off, Y3off);
            Point Point4 = new Point(X4off, Y4off);
            points = new PointCollection();
            points.Add(Point1);
            points.Add(Point2);
            points.Add(Point3);
            points.Add(Point4);
        }

        public void initArea(double w, double h)
        {
            initpointh = h;
            initpointw = w;
            X1off = w * 0.25 ;
            X2off = w * 0.75 ;
            X3off = w * 0.75 ;
            X4off = w * 0.25 ;
            Y1off = h * 0.25 ;
            Y2off = h * 0.25 ;
            Y3off = h * 0.75 ;
            Y4off = h * 0.75 ;

        }

        public double BtnTransX
        {
            get { return (X1off + X2off + X3off + X4off +2*offset) / 4 ; }
        }

        public double BtnTransY
        {
            get { return (Y1off + Y2off + Y3off + Y4off +2*offset) / 4 ; }
        }

        public double BtnCropX
        {
            get { return (X1off  + X3off +  offset) / 2; }
        }

        public double BtnCropY
        {
            get { return (Y1off  + Y3off  + offset) / 2; }
        }

        public PointCollection TransformPoints
        {
            get
            {
                Point Point1 = new Point(X1off + offset / 2, Y1off + offset / 2);
                Point Point2 = new Point(X2off + offset / 2, Y2off + offset / 2);
                Point Point3 = new Point(X3off + offset / 2, Y3off + offset / 2);
                Point Point4 = new Point(X4off + offset / 2, Y4off + offset / 2);
                points = new PointCollection();
                points.Add(Point1);
                points.Add(Point2);
                points.Add(Point3);
                points.Add(Point4);
                return points;
            }
        }

        public double CropX1 {
            get { return (X1off < X3off ? X1off : X3off) + offset / 2; }
        }

        public double CropY1
        {
            get { return (Y1off < Y3off ? Y1off : Y3off) + offset / 2; }
        }

        public double CropWidth
        {
            get { return Math.Abs(X3off - X1off); }
        }


        public double CropHeight
        {
            get { return Math.Abs(Y3off - Y1off); }
        }
        
        
        public Visibility CommonCompVis
        {
            get
            { return mode == FunctionMode.opt0 ? Visibility.Collapsed : Visibility.Visible; }
        }

        public Visibility CropOnlyVis
        {
            get
            { return mode == FunctionMode.crop ? Visibility.Visible : Visibility.Collapsed; }
        }

        public Visibility TransOnlyVis
        {
            get
            { return mode == FunctionMode.transform ? Visibility.Visible : Visibility.Collapsed; }
        }


        public double X1off
        {
            get { return x1off; }
            set { x1off = value;
            onPropertyChanged("X1off");
            onPropertyChanged("CropX1");
            onPropertyChanged("CropWidth");
            onPropertyChanged("TransformPoints");
            onPropertyChanged("BtnTransX");
            onPropertyChanged("BtnCropX");
            }
        }


        public double X2off
        {
            get { return x2off; }
            set { x2off = value;
            onPropertyChanged("X2off");
            onPropertyChanged("TransformPoints");
            onPropertyChanged("BtnTransX");
            onPropertyChanged("BtnCropX");
            }
        }


        public double X3off
        {
            get { return x3off; }
            set { x3off = value;
            onPropertyChanged("X3off");
            onPropertyChanged("CropX1");
            onPropertyChanged("CropWidth");
            onPropertyChanged("TransformPoints");
            onPropertyChanged("BtnTransX");
            onPropertyChanged("BtnCropX");
            }
        }


        public double X4off
        {
            get { return x4off; }
            set { x4off = value;
            onPropertyChanged("X4off");
            onPropertyChanged("TransformPoints");
            onPropertyChanged("BtnTransX");
            onPropertyChanged("BtnCropX");
            }
        }

        public double Y1off
        {
            get { return y1off; }
            set { y1off = value;
            onPropertyChanged("Y1off");
            onPropertyChanged("CropY1");
            onPropertyChanged("CropHeight");
            onPropertyChanged("TransformPoints");
            onPropertyChanged("BtnTransY");
            onPropertyChanged("BtnCropY");
            }
        }

        public double Y2off
        {
            get { return y2off; }
            set { y2off = value;
            onPropertyChanged("Y2off");
            onPropertyChanged("TransformPoints");
            onPropertyChanged("BtnTransY");
            onPropertyChanged("BtnCropY");
            }
        }

        public double Y3off
        {
            get { return y3off; }
            set { y3off = value;
            onPropertyChanged("Y3off");
            onPropertyChanged("CropY1");
            onPropertyChanged("CropHeight");
            onPropertyChanged("TransformPoints");
            onPropertyChanged("BtnTransY");
            onPropertyChanged("BtnCropY");
            }
        }

        public double Y4off
        {
            get { return y4off; }
            set { y4off = value;
            onPropertyChanged("Y4off");
            onPropertyChanged("TransformPoints");
            onPropertyChanged("BtnTransY");
            onPropertyChanged("BtnCropY");
            }
        }

        public FunctionMode Mode
        {
            get { return mode; }
            set { mode = value;
            initArea(initpointw, initpointh);
            onPropertyChanged("CommonCompVis");
            onPropertyChanged("CropOnlyVis");
            onPropertyChanged("TransOnlyVis");
            }
        }


    }
}
